<?php

require dirname(dirname(__FILE__)) . '/includes/common_head.php';

$smarty->template_dir = $app_root . $site['home_dir'] . '/templates';
$smarty->compile_id   = $app_root . $site['home_dir'] . '/templates';

$smarty->assign('resources', "{$site['home_dir']}/resources");
require 'modifier.var.php';


for($i=count($path);$i>0;$i--) {
	if (file_exists(str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i)) . '/index.php')))) {
		$logger->debug(str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i)) . '/index.php')));
		$handler = str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i)) . '/index.php'));
		break;
	} elseif (file_exists(str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i-1))) . '/' . $path[$i-1] . '.php'))) {
		$logger->debug(str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i-1))) . '/' . $path[$i-1] . '.php'));
		$handler = str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i-1))) . '/' . $path[$i-1] . '.php');
		break;
	} elseif (file_exists(str_replace('//', '/', $app_root . '/admin/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i)) . '/index.php')))) {
		$logger->debug(str_replace('//', '/', $app_root . '/admin/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i)) . '/index.php')));
		$handler = str_replace('//', '/', $app_root . '/admin/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i)) . '/index.php'));
		break;
	} elseif (file_exists(str_replace('//', '/', $app_root . '/admin/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i-1))) . '/' . $path[$i-1] . '.php'))) {
		$logger->debug(str_replace('//', '/', $app_root . '/admin/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i-1))) . '/' . $path[$i-1] . '.php'));
		$handler = str_replace('//', '/', $app_root . '/admin/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i-1))) . '/' . $path[$i-1] . '.php');
		break;
	}
}

if (isset($handler)) {
	require $handler;
} else {
	if (file_exists($app_root . $site['home_dir'] . '/admin-custom/frontend/fallback.php')) {
		require $app_root . $site['home_dir'] . '/admin-custom/frontend/fallback.php';
	} else {
		require $app_root . '/admin/frontend/fallback.php';
	}
}

require dirname(dirname(__FILE__)) . '/includes/common_foot.php';
